package com.hisoft.entity;

import java.util.List;

import org.apache.log4j.Logger;

import com.hisoft.type.ResultEnum;

public class Choose extends ABaseTag {
	Logger logger = Logger.getLogger(Choose.class);
	//when对象
	private ABaseTag when;
	//do对象
	private List<ABaseTag> chooseDo;
	//otherwise对象
	private List<ABaseTag> otherwise;

	public ABaseTag getWhen() {
		return when;
	}

	public void setWhen(ABaseTag when) {
		this.when = when;
	}

	public List<ABaseTag> getDo() {
		return chooseDo;
	}

	public void setDo(List<ABaseTag> do1) {
		chooseDo = do1;
	}

	public List<ABaseTag> getOtherwise() {
		return otherwise;
	}

	public void setOtherwise(List<ABaseTag> otherwise) {
		this.otherwise = otherwise;
	}

	@Override
	public String excute() {
		//数据第二次初始化
		this.beforeExcute();
		logger.info(toString());
		logger.info(WhenToString());
		boolean flag = true;
		//when执行
		when.excute();
		//判断when是否执行成功，是就执行Do，否则就执行OTHERWise
		if (when.getResult().equals(ResultEnum.PASS)) {
			logger.info(ChooseDoToString());
			if (chooseDo == null || chooseDo.isEmpty()) {
			} else {
				for (ABaseTag temp : chooseDo) {
					flag = flag & (temp.excute().contains("" + ResultEnum.PASS));
				}
			}
		} else {
			logger.info(OtherwiseToString());
			if (otherwise == null || otherwise.isEmpty()) {
			} else {
				for (ABaseTag temp : otherwise) {
					flag = flag & temp.excute().contains("" + ResultEnum.PASS);
				}
			}
		}
		// this.setResult(flag);
		if (flag == true) {
			setResult(ResultEnum.PASS);
			return "" + ResultEnum.PASS;
		} else {
			return "" + ResultEnum.FAIL;
		}

	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "[ " + WhenToString() + " ]\n[ " + ChooseDoToString() + " ]\n[ " + OtherwiseToString() + " ]";
	}

	public String WhenToString() {
		return "When " + " | " + when.toString();
	}

	public String ChooseDoToString() {
		String str = "Do ";
		for (Object o : chooseDo) {
			str += o.toString() + "\n";
		}
		return str;
	}

	public String OtherwiseToString() {
		String str = "Otherwise ";
		for (Object o : otherwise) {
			str += o.toString() + "\n";
		}
		return str;

	}
}
